//
//  RankListNormalCell.swift
//  MM2
//
//  Created by 李成明 on 2022/1/7.
//

import UIKit

class RankListNormalCell: UITableViewCell {
    
    static let cellHeight: CGFloat = 70
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        selectionStyle = .none
        backgroundColor = .clear
        layer.masksToBounds = true
        layer.cornerRadius = 10
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var model: RankListModel? {
        didSet {
            updateUI()
        }
    }
    
    private lazy var bgView: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 10
        view.alpha = 0.3
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 16)
        view.numberOfLines = 0
        view.textAlignment = .center
        return view
    }()
    
    private lazy var rankNumLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: faceValueFontStr, size: 24)
        view.textAlignment = .center
        return view
    }()
    
    private lazy var avatar: UIImageView = {
        let view = UIImageView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 20
        return view
    }()
    
    private lazy var faceValueLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont(name: faceValueFontStr, size: 30)
        view.textColor = faceValueColor
        return view
    }()
}

extension RankListNormalCell {
    private func setupUI() {
        contentView.addsubViews([bgView, nameLabel, rankNumLabel, avatar, faceValueLabel])
        bgView.snp.makeConstraints { make in
            make.left.top.right.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-8)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(16)
            make.centerY.equalTo(contentView)
            make.width.equalTo(50)
        }
        
        rankNumLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(30)
            make.centerY.equalTo(contentView)
            make.size.equalTo(CGSize(width: 32, height: 32))
        }
        
        avatar.snp.makeConstraints { make in
            make.left.equalTo(rankNumLabel.snp.right).offset(45)
            make.centerY.equalTo(contentView)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        faceValueLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(40)
            make.top.right.bottom.equalTo(contentView)
        }
    }
    
    private func updateUI() {

        guard let model = model else { return }
        nameLabel.text = model.userName
        rankNumLabel.text = "\(model.rankNum)"
        bgView.image = (model.rankNum%2 == 0) ? UIImage(named: "rank_red_bgimg") : UIImage(named: "rank_green_bgimg")
        
        avatar.setAvatar(urlStr: model.avatar)
        
        let attrStr = NSMutableAttributedString.init(string: String(format: "%.2f", model.faceValue),attributes: [.font: UIFont.systemFont(ofSize: 14)])
        attrStr.addAttributes([.font: UIFont(name: "Verdana-Italic", size: 50)!], range: NSRange(location: 0, length: model.faceValue > 10 ? 2 : 1))
        faceValueLabel.attributedText = attrStr
    }
}
